<?php
class Category_model extends MY_Model {
	private $module = 'category';
	private $table = 'category';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('contentTxt')!='' && $this->input->post('contentTxt')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('contentTxt').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('contentTxt')!='' && $this->input->post('contentTxt')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results($this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa?
			$checkData = $this->checkData($this->input->post('titleAdmincp'));
			if($checkData){
				print 'error-title-exists';
				exit;
			}
			
			if($this->input->post('titleAdmincp') == NULL){
				print 'empty-title-exists';
				exit;	
			}
			
			if($this->input->post('folderAdmincp') == NULL){
				print 'empty-folder-exists';
				exit;	
			}
			mkdir( DIR_UPLOAD_PRODUCTS.strtoupper($this->input->post('folderAdmincp'))."" );
			
			$data = array(
				'title'=> $this->input->post('titleAdmincp'),
				'slug'=> $this->input->post('slugAdmincp'),
				'order'=> $this->input->post('orderAdmincp'),
				'folder'=> strtoupper($this->input->post('folderAdmincp')),
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert($this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			//Kiểm tra đã tồn tại chưa?
			if($result[0]->title!=$this->input->post('titleAdmincp')){
				$checkData = $this->checkData($this->input->post('titleAdmincp'));
				if($checkData){
					print 'error-title-exists';
					exit;
				}
			}
			
			if($result[0]->slug!=$this->input->post('slugAdmincp')){
				$checkSlug = $this->checkSlug($this->input->post('slugAdmincp'));
				if($checkSlug){
					print 'error-slug-exists';
					exit;
				}
			}
			
			if($this->input->post('folderAdmincp') != $result[0]->folder){
				rmdir(DIR_UPLOAD_PRODUCTS.$result[0]->folder);
				mkdir( DIR_UPLOAD_PRODUCTS.strtoupper($this->input->post('folderAdmincp'))."" );	
			}
			
			$data = array(
				'title'=> $this->input->post('titleAdmincp'),
				'slug'=> $this->input->post('slugAdmincp'),
				'order'=> $this->input->post('orderAdmincp'),
				'folder'=> strtoupper($this->input->post('folderAdmincp')),
				'status'=> $status,
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update($this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function checkData($title){
		$this->db->select('id');
		$this->db->where('title',$title);
		$this->db->limit(1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}
	
	function checkSlug($slug){
		$this->db->select('id');
		$this->db->where('slug',$slug);
		$this->db->limit(1);
		$query = $this->db->get($this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}
	
	function getCategoryAdmin(){
		$this->db->where('status',1);
		$query=$this->db->get($this->table);
		
		if($query->result()){
			return $query->result();	
		}else{
			return false;	
		}
	}
	
	/*----------------------FRONTEND----------------------*/
	function getCategory(){
		$this->db->where('status',1);
		$this->db->order_by('order','ASC');
		$query=$this->db->get($this->table);
		
		if($query->result()){
			return $query->result();	
		}else{
			return false;	
		}
	}
	
	function getFolder($id){
		$this->db->select('folder');
		$this->db->where('id',$id);
		$query=$this->db->get($this->table);
		
		if($query->result()){
			return $query->result();	
		}else{
			return false;	
		}
	}
	
	function getTitle($id){
		$this->db->select('title');
		$this->db->where('id',$id);
		$query=$this->db->get($this->table);
		
		if($query->result()){
			return $query->result();	
		}else{
			return false;	
		}
	}

	/*--------------------END FRONTEND--------------------*/
}